<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点击按钮随机显示颜色</title>
    <style>
        img {
            display: block;
            width: 500px;
            height: 350px;
        }
    </style>
</head>

<body>
    <img src="../images/1.webp" alt=""><br>
    <button>点击切换图片</button>
</body>

<script>
    // 声明图片数组
    const imgArr = [
        '../images/1.webp',
        '../images/2.webp',
        '../images/3.webp',
        '../images/4.webp',
    ];
    // 获取标签元素
    const img = document.querySelector('img');
    const btn = document.querySelector('button');

    // 为按钮注册绑定事件
    btn.addEventListener('click', function (e) {
        // 设置img图片src链接
        img.src = `../images/${getRandom(1, imgArr.length)}.webp`;
    })

    // 封装随机函数
    function getRandom(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }
</script>

</html>